﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTUDW_CNTN2010_F_CDT.Models;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    [Authorize]
    public class CheckOutController : Controller
    {
        EcommerceDBContext db = new EcommerceDBContext();

        public ActionResult FillOrder()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FillOrder(Order order)
        {
            if (ModelState.IsValid)
            {
                order.UserName = User.Identity.Name;
                order.OrderDate = DateTime.Now;

                db.Orders.Add(order);
                db.SaveChanges();

                ShoppingCart cart = ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(order);

                return RedirectToAction("Index", "Home");
            }

            return View(order);
        }
    }
}
